package com.luobr.backend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.luobr.backend.annotation.AuthCheck;
import com.luobr.backend.common.BaseResponse;
import com.luobr.backend.common.ResponseCode;
import com.luobr.backend.common.ResultUtils;
import com.luobr.backend.constant.UserConstant;
import com.luobr.backend.exception.ThrowUtils;
import com.luobr.backend.model.dto.user.*;
import com.luobr.backend.model.entity.User;
import com.luobr.backend.model.vo.user.UserLoginVO;
import com.luobr.backend.model.vo.user.UserQueryVO;
import com.luobr.backend.service.UserService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;


/**
 * @author: 罗念笙
 * @date: 2025/2/20 16:03
 * 操作用户控制层
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    /**
     * 用户注册
     */
    @ResponseBody
    @PostMapping("/register")
    public BaseResponse<Long> registerUser(@RequestBody UserRegisterRequest userRegisterRequest) {
        // 校验参数
        ThrowUtils.throwIf(userRegisterRequest == null, ResponseCode.PARAMS_ERROR);
        // 返回用户id
        long userId = userService.registerUser(userRegisterRequest.getUserAccount(),
                userRegisterRequest.getUserPassword(), userRegisterRequest.getCheckPassword());
        return ResultUtils.success(userId);
    }

    /**
     * 用户登录
     */
    @ResponseBody
    @PostMapping("/login")
    public BaseResponse<UserLoginVO> loginUser(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        // 校验参数
        ThrowUtils.throwIf(userLoginRequest == null, ResponseCode.PARAMS_ERROR);
        // 返回用户信息
        UserLoginVO userLoginVO = userService.loginUser(userLoginRequest.getUserAccount(), userLoginRequest.getUserPassword(), request);
        return ResultUtils.success(userLoginVO);
    }

    /**
     * 获取登录信息
     */
    @ResponseBody
    @GetMapping("/get/login")
    public BaseResponse<UserLoginVO> getLoginUser(HttpServletRequest request) {
        return ResultUtils.success(userService.getLoginUser(request));
    }

    /**
     * 用户登出
     */
    @ResponseBody
    @PostMapping("/logout")
    public BaseResponse<Boolean> logoutUser(HttpServletRequest request) {
        return ResultUtils.success(userService.logoutUser(request));
    }

    /**
     * 新增用户（管理员权限）
     */
    @PostMapping("/add")
    @AuthCheck(roleValue = UserConstant.USER_ROLE.ADMIN)
    public BaseResponse<Long> addUser(@RequestBody UserAddRequest userAddRequest) {
        ThrowUtils.throwIf(userAddRequest == null, ResponseCode.PARAMS_ERROR);
        Long userId = userService.addUser(userAddRequest);
        return ResultUtils.success(userId);
    }

    /**
     * 删除用户（管理员权限）
     */
    @PostMapping("/delete")
    @AuthCheck(roleValue = UserConstant.USER_ROLE.ADMIN)
    public BaseResponse<Boolean> deleteUser(long id) {
        ThrowUtils.throwIf(id <= 0, ResponseCode.PARAMS_ERROR);
        boolean result = userService.removeById(id);
        return ResultUtils.success(result);
    }

    /**
     * 更新用户（管理员权限）
     */
    @PostMapping("/update")
    @AuthCheck(roleValue = UserConstant.USER_ROLE.ADMIN)
    public BaseResponse<Boolean> updateUser(@RequestBody UserUpdateRequest userUpdateRequest) {
        ThrowUtils.throwIf(userUpdateRequest == null || userUpdateRequest.getId() == null, ResponseCode.PARAMS_ERROR);
        User user = new User();
        BeanUtils.copyProperties(userUpdateRequest, user);
        ThrowUtils.throwIf(!userService.updateById(user), ResponseCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    /**
     * 获取用户信息（管理员权限）
     */
    @GetMapping("/get/vo")
    @AuthCheck(roleValue = UserConstant.USER_ROLE.ADMIN)
    public BaseResponse<UserQueryVO> getUserVOById(long id) {
        ThrowUtils.throwIf(id <= 0, ResponseCode.PARAMS_ERROR);
        User user = userService.getById(id);
        ThrowUtils.throwIf(user == null, ResponseCode.NOT_FOUND_ERROR);
        return ResultUtils.success(UserQueryVO.objToQueryVo(user));
    }

    /**
     * 获取用户信息分页列表（管理员权限）
     */
    @PostMapping("/list/page/vo")
    @AuthCheck(roleValue = UserConstant.USER_ROLE.ADMIN)
    public BaseResponse<Page<UserQueryVO>> listUserVOByPage(@RequestBody UserQueryRequest userQueryRequest) {
        ThrowUtils.throwIf(userQueryRequest == null, ResponseCode.PARAMS_ERROR);
        return ResultUtils.success(userService.getUserQueryVOPage(userQueryRequest));
    }
}
